package com.example.administrator.solution;

import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SettingsActivity extends AppCompatActivity {

    TextView vwServerAddr;
    TextView vwServerPort;
    Button vwSaveSettings;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }

        vwServerAddr = findViewById(R.id.vw_server_addr);
        vwServerPort = findViewById(R.id.vw_server_port);
        vwSaveSettings = findViewById(R.id.vw_save_settings);

        SharedPreferences sp = this.getSharedPreferences("settings", Activity.MODE_PRIVATE);
        vwServerAddr.setText(sp.getString("hostname","192.168.1.106"));
        vwServerPort.setText(sp.getString("port","60000"));
        vwSaveSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });

    }


    private void save(){
        String address = vwServerAddr.getText().toString();
        String port = vwServerPort.getText().toString();

        SharedPreferences sp = this.getSharedPreferences("settings", Activity.MODE_PRIVATE);

        SharedPreferences.Editor edit = sp.edit();
        edit.putString("hostname", address);
        edit.putString("port", port);
        edit.apply();


        this.finish();
    }


}
